﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using QLChoDiaOc.Process;

namespace QLChoDiaOc.Forms
{
    /// <summary>
    /// Interaction logic for FormDsNhanVien.xaml
    /// </summary>
    public partial class FormDsNhanVien : Page
    {
        ShowInfo _showInfo = new ShowInfo();
        SelectInfo _selectInfo = new SelectInfo();
        UpdateInfo _updateInfo = new UpdateInfo();
        static bool m_bSelectData = false;

        public FormDsNhanVien()
        {
            InitializeComponent();
            _showInfo.ShowNhanVien(dataNhanVien);
            AddComboBox();
        }

        void AddComboBox()
        {
            loaiNhanVienComboBox.Items.Clear();
            for (int i = 0; i < _selectInfo.SelectAllTenLoaiNH().Count; i++)
                loaiNhanVienComboBox.Items.Add(_selectInfo.SelectAllTenLoaiNH()[i]);
        }

        private void btnThem_Click(object sender, RoutedEventArgs e)
        {
            maNhanVientextbox.Text = "";
            hoTenTextBox.Text="";
            loaiNhanVienComboBox.SelectedItem = null;

            WinThemNhanVien _temp = new WinThemNhanVien();
            _temp.Show();
        }

        private void bntReloadNhanVien_Click(object sender, RoutedEventArgs e)
        {
            if (!m_bSelectData)
                m_bSelectData = true;
            maNhanVientextbox.Text = "";
            hoTenTextBox.Text = "";
            loaiNhanVienComboBox.SelectedItem = null;
            _showInfo = new ShowInfo();
            _showInfo.ShowNhanVien(dataNhanVien);
        }

        private void btnSuaNhanVien_Click(object sender, RoutedEventArgs e)
        {
            if (hoTenTextBox.Text == "" || loaiNhanVienComboBox.SelectedItem == null)
                MessageBox.Show("Chưa nhập đủ thông tin", "Thông Báo");
            else
            {
                _updateInfo.UpdateNhanVien(maNhanVientextbox.Text, hoTenTextBox.Text, _selectInfo.SelectMaLoaiNhanVien(loaiNhanVienComboBox.SelectedItem.ToString()));
                if (!m_bSelectData)
                    m_bSelectData = true;
                maNhanVientextbox.Text = "";
                hoTenTextBox.Text = "";
                loaiNhanVienComboBox.SelectedItem = null;
                dataNhanVien.ItemsSource = null;
                _showInfo = new ShowInfo();
                _showInfo.ShowNhanVien(dataNhanVien);
            }
        }

        private void dataNhanVien_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (!m_bSelectData)
            {
                var item = dataNhanVien.SelectedItem as NHANVIEN;

                maNhanVientextbox.Text = item.MaNhanVien;
                hoTenTextBox.Text = item.HoTenNV;
                loaiNhanVienComboBox.SelectedItem = _selectInfo.SelectTenLoaiNhanVien(item.MaLoaiNV);
            }
            else
            {
                m_bSelectData = false;
                _showInfo = new ShowInfo();
                _showInfo.ShowLoaiNhanVien(dataNhanVien);
            }
        }
    }
}
